#include "setwig.h"
#include "ui_setwig.h"

SetWig::SetWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWig)
{
    ui->setupUi(this);
    ui->pushButton_Issued->setEnabled(false);
    connect(ui->pushButton_Connect, SIGNAL(clicked()), this, SLOT(btnClickedConnectSlot()));
    connect(ui->pushButton_Issued, SIGNAL(clicked()), this, SLOT(btnClickedIssuedSlot()));
    connect(ui->pushButton_Refresh, SIGNAL(clicked()), this, SLOT(btnClickedRefreshSlot()));
    connect(ui->pushButton_change, SIGNAL(clicked()), this, SLOT(btnClickedchangeSlot()));
    connect(ui->pushButton_Regulartime, SIGNAL(clicked()), this, SLOT(btnClickedRegulartimeSlot()));
}
// 用于接收MQTT的槽函数
void SetWig::dataUpSlot(QByteArray Array)
{
    QString payload = Array;
}
// 点击连接的槽函数
void SetWig::btnClickedConnectSlot()
{
    QString ip = ui->lineEdit_IP->text();
    QString portText = ui->lineEdit_Port->text();

    if (ip.isEmpty() || portText.isEmpty())
    {
        QMessageBox::warning(this, "错误", "IP和端口号不能为空");
        return;
    }
    bool ok;
    int port = portText.toInt(&ok);
    if (!ok)
    {
        QMessageBox::warning(this, "错误", "端口号无效");
        return;
    }
    connect(GlobalVal::mqtt, SIGNAL(ConnectBoolerr(int)), this, SLOT(boolConnectStatusSlot(int)));
    GlobalVal::mqtt->setHost(QHostAddress(ip));
    GlobalVal::mqtt->setPort(port);
    GlobalVal::mqtt->connectToHost();
}
void SetWig::boolConnectStatusSlot(int num)
{
    if(num == 0)
    {
        ui->pushButton_Issued->setEnabled(false);
        QMessageBox::warning(this, "error", "请输入有效的IP和端口号");
    }
    else if(num == 2)
    {
        ui->pushButton_Issued->setEnabled(true);
        QMessageBox::warning(this, "成功", "连接成功");
    }
    // 判断按钮是否可用
    Issuednum = num;
}
// 点击下发配置
void SetWig::btnClickedIssuedSlot()
{
    // 打开data.json 文件
    QFile file("E:/Project_group1/data.json");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "打开失败";
        return;
    }
    QByteArray data = file.readAll();
    file.close();
    // 发送命令
    QMQTT::Message message;
    message.setTopic("/app/control/up");
    message.setPayload(data);
    GlobalVal::mqtt->publish(message);
}

// 点击刷新上报的槽函数
void SetWig::btnClickedRefreshSlot()
{
    if(Issuednum == 0)
    {
        QMessageBox::warning(this, "错误", "设置失败，未连接服务器");
    }
    else if(Issuednum == 2)
    {
        QMessageBox::information(this, "成功", "设置成功");
        // 修改上报模式为刷新上报
        QByteArray data = "{ \"pattern\" :0,\"mode\":2} ";
        // 用于获取解析的JSON数据时的错误信息
        QJsonParseError jsonerror;  // 创建一个JSON解析错误对象
        QJsonDocument doc = QJsonDocument::fromJson(data, &jsonerror); // 将JSON数据解析为QJsonDocument对象
        if(doc.isNull())
        {
            qDebug() << "错误的JSON";
            return;
        }
        QByteArray jsonData = doc .toJson();
        qDebug() << "jsondata" << jsonData;

        // 发送命令
        QMQTT::Message message;
        message.setTopic("down");
        message.setPayload(doc.toJson());
        GlobalVal::mqtt->publish(message);
    }
}
// 点击变化上报的槽函数
void SetWig::btnClickedchangeSlot()
{
    if(Issuednum == 0)
    {
        QMessageBox::warning(this, "错误", "设置失败，未连接服务器");
    }
    else if(Issuednum == 2)
    {
        QMessageBox::information(this, "成功", "设置成功");
        // 修改上报模式为变化上报
        QByteArray data = "{ \"pattern\" :0,\"mode\":3} ";
        // 用于获取解析的JSON数据时的错误信息
        QJsonParseError jsonerror;  // 创建一个JSON解析错误对象
        QJsonDocument doc = QJsonDocument::fromJson(data, &jsonerror); // 将JSON数据解析为QJsonDocument对象
        if(doc.isNull())
        {
            qDebug() << "错误的JSON";
            return;
        }
        QByteArray jsonData = doc .toJson();
        qDebug() << "jsondata" << jsonData;

        // 发送命令
        QMQTT::Message message;
        message.setTopic("down");
        message.setPayload(doc.toJson());
        GlobalVal::mqtt->publish(message);
    }
}
// 点击定时上报的槽函数
void SetWig::btnClickedRegulartimeSlot()
{
    if(Issuednum == 0)
    {
        QMessageBox::warning(this, "错误", "设置失败，未连接服务器");
    }
    else if(Issuednum == 2)
    {
        QMessageBox::information(this, "成功", "设置成功");
        // 修改上报模式为定时上报
        QByteArray data = "{ \"pattern\" :0,\"mode\":1} ";
        // 用于获取解析的JSON数据时的错误信息
        QJsonParseError jsonerror;  // 创建一个JSON解析错误对象
        QJsonDocument doc = QJsonDocument::fromJson(data, &jsonerror); // 将JSON数据解析为QJsonDocument对象
        if(doc.isNull())
        {
            qDebug() << "错误的JSON";
            return;
        }
        QByteArray jsonData = doc .toJson();
        qDebug() << "jsondata" << jsonData;

        // 发送命令
        QMQTT::Message message;
        message.setTopic("down");
        message.setPayload(doc.toJson());
        GlobalVal::mqtt->publish(message);
    }
}
SetWig::~SetWig()
{
    delete ui;
}
